package com.jiantong.lawEnforcing.impl.manager;

import com.jiantong.common.util.UserUtil;
import com.jiantong.lawEnforcing.dto.CasesBasicDto;
import com.jiantong.lawEnforcing.enums.LawCasesStatus;
import com.jiantong.lawEnforcing.enums.LawCasesType;
import com.jiantong.lawEnforcing.impl.dao.CasesBasicDao;
import com.jiantong.lawEnforcing.impl.dao.CasesCheckDao;
import com.jiantong.lawEnforcing.impl.dao.PartiesInformationDao;
import com.jiantong.lawEnforcing.impl.dao.SimplenessCasesDao;
import com.jiantong.lawEnforcing.model.CasesBasic;
import com.jiantong.lawEnforcing.model.CasesCheck;
import com.jiantong.lawEnforcing.model.PartiesInformation;
import com.jiantong.lawEnforcing.model.SimplenessCases;
import io.terminus.common.model.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 执法办案相关事务处理
 * Created by mz03 on 2017/8/25.
 */
@Component
public class CasesBasicManager {
    private final CasesBasicDao casesBasicDao;
    private final PartiesInformationDao partiesInformationDao;
    private final SimplenessCasesDao simplenessCasesDao;
    private final CasesCheckDao casesCheckDao;
    @Autowired
    public CasesBasicManager(CasesBasicDao casesBasicDao, PartiesInformationDao partiesInformationDao, SimplenessCasesDao simplenessCasesDao, CasesCheckDao casesCheckDao) {
        this.casesBasicDao = casesBasicDao;
        this.partiesInformationDao = partiesInformationDao;
        this.simplenessCasesDao = simplenessCasesDao;
        this.casesCheckDao = casesCheckDao;
    }


    /**
     * 创建一条信息casesBasicDto
     * @param casesBasicDto
     * @return
     */
    @Transactional
    public Long createCasesBasic(CasesBasicDto casesBasicDto){

        //需要事务处理 先根据Dto中的当事人信息创建当事人PartiesInformation, 在根据当事人创建案件信息casesBasic
        CasesBasic casesBasic = casesBasicDto.getCasesBasic();
        PartiesInformation partiesInformation = casesBasicDto.getPartiesInformation();
        partiesInformationDao.create(partiesInformation);


        //生成案件编号
        String cases_number = "LAW-"+ new Date().getTime();
        casesBasic.setCases_number(cases_number);
        casesBasic.setReceiver_id(UserUtil.getUserId());//设置案件办理人id: receiver_id organization_name
        casesBasic.setParties_id(partiesInformation.getId());//当事人id
        //根据当事人类型设置name
        Integer type = partiesInformation.getParties_type();
        casesBasic.setParties_type(type);
        switch (type){
            case 1 :
                casesBasic.setParties_name(partiesInformation.getPersonage_name());
                break;
            case 2 :
                casesBasic.setParties_name(partiesInformation.getOrganization_name());
                break;

            default:
                break;
        }
        casesBasicDao.create(casesBasic);
        return casesBasic.getId();
    }

    /**
     * 更新casesBasic时自动创建对应的当场处罚或立案审批单据
     * @param casesBasic
     * @return
     */
    @Transactional
    public Boolean updateCasesBasic(CasesBasic casesBasic){
        CasesBasic casesBasic1 = casesBasicDao.findById(casesBasic.getId());
        casesBasic.setParties_type(casesBasic1.getParties_type());
        casesBasic.setParties_name(casesBasic1.getParties_name());
        //需要生成当场处罚单据
        if (casesBasic.getCases_type().equals(LawCasesType.SIMPLENESS_CASES.value()) && casesBasic.getStatus() == LawCasesStatus.RECEPTION.value()) {     //更新后cases_type为简单案件
            //查到未更新前的案件的status
            Integer type = casesBasicDao.findById(casesBasic.getId()).getCases_type();
            //判断更新之前是否为新接的案子
            if (!type.equals(LawCasesType.SIMPLENESS_CASES.value())) {     //更新前的cases_type不为简单案件
                //创建当场处罚单据APIs --> service --> serviceImpl  --> manager --> dao --> mapper.xml --> MySQL

                //根据casesBasic新建一个当场处罚单
                SimplenessCases simplenessCases = new SimplenessCases();
                simplenessCases.setCases_basic_id(casesBasic.getId());
                simplenessCases.setParties_name(casesBasic.getParties_name());
                simplenessCases.setCause_action(casesBasic.getCause_action());
                simplenessCases.setCases_source(casesBasic.getCases_source());
                simplenessCases.setParties_type(casesBasic.getParties_type());
                if (simplenessCases.getStatus() == null) {
                    simplenessCases.setStatus(1);
                }
                //添加这条当场处罚单
                simplenessCasesDao.create(simplenessCases);
                simplenessCases.getId();
            }
        }

        //需要创建立案审批
        if (casesBasic.getCases_type().equals(LawCasesType.CASES_CHECK.value())&& casesBasic.getStatus() == LawCasesStatus.RECEPTION.value()) {//更新后未一般案件
            Integer type = casesBasicDao.findById(casesBasic.getId()).getCases_type();
            if (!type.equals(LawCasesType.CASES_CHECK.value())) {     //更新前的cases_type不为一般案件
                //创建立案审批;
                CasesCheck casesCheck = new CasesCheck();
                casesCheck.setCases_basic_id(casesBasic.getId());
                casesCheck.setParties_name(casesBasic.getParties_name());
                casesCheck.setParties_type(casesBasic.getParties_type());
                casesCheck.setCases_source(casesBasic.getCases_source());
                casesCheck.setCause_action(casesBasic.getCause_action());
                if (casesCheck.getStatus() == null) {
                    casesCheck.setStatus(0);
                }
                //添加立案审批
                casesCheckDao.create(casesCheck);
                casesCheck.getId();
            }
        }
        //更新案件基本信息
        if (casesBasic.getStatus() == null) {
            casesBasic.setStatus(0);
        }
        casesBasicDao.update(casesBasic);
        return casesBasic.getId()!=null;
    }

}
